<template>
	<view class="address-manager-page">
		<text class="add-addr" @click="addAddress">添加新地址</text>
		<view class="address-list">
			<template v-if="addrList.length > 0">
				<!--  @click.stop -->
				<view class="each-address" v-for="(item,index) of addrList" :key="item.id" @click="backAddr(item)">
					<view class="name-phone">
						<text>收货人：{{item.realName}}</text><text>{{item.contactPhone}}</text>
					</view>
					<view class="addr">{{item.province}}{{item.city}}{{item.area}}{{item.detailAddress}}</view>
					<view class="opera-btns">
						<view class="left-default-btn" @click.stop>
							<checkbox-group @change="defaultChange(item)">
								<label>
									<checkbox class="custom-checkbox" value="defaultAddr"
										:checked="item.isDefault === '1'" style="transform:scale(0.7)" /><text
										class="default-addr">默认地址</text>
								</label>
							</checkbox-group>
						</view>
						<view class="right-edit-delete">
							<view class="btn-text edit" @click.stop="editEvent(item)">
								<!-- <image class="icon-edit-delete icon-edit" :src="iconEdit"></image> -->
								<text>编辑</text>
							</view>
							<view class="btn-text delete" @click.stop="deleteEvent(item)">
								<!-- <image class="icon-edit-delete icon-delete" :src="iconDelete"></image> -->
								<text>删除</text>
							</view>
						</view>
					</view>
				</view>
			</template>
			<template v-else>
				<!-- <image class="no-data-img" mode="aspectFit" src="/static/image/icon-noData.png"></image> -->
			</template>
		</view>
	</view>
</template>

<script>
	const api = require('../../config/api')
	const util = require('../../utils/util')
	export default {
		name: 'addressManager',
		data() {
			return {
				addrList: [{
					id: '1',
					realName: '张三',
					contactPhone: '15688865545',
					province: '山东省',
					city: '济南市',
					area: '槐荫区',
					detailAddress: '白马山街道12044号',
					isDefault: '1'
				}],
				// iconEdit: '/static/icon/icon-card.png',
				// iconDelete: '/static/icon/icon-pay.png',
				isFromToBuy: false
			}
		},
		onShow() {
			this.getAllAddr()
		},
		onPullDownRefresh() {
			this.getAllAddr()
		},

		onLoad(option) {
			if (option?.fromToBuy) {
				this.isFromToBuy = true
			}
			uni.$on('setRichText', (data) => {
				this.setRichHtml(data)
			})
			const eventChannel = this.getOpenerEventChannel()

			// 监听从原页面发送过来的事件
			eventChannel.on('acceptDataFromOpenerPage', (data) => {
				// 在这里处理接收到的数据
				// console.log('从原页面接收到的数据：', data);

				this.receivedData = data.data // 假设你只对 data.data 感兴趣
			})
		},
		methods: {
			backAddr(item) {
				console.log(item)
				// console.log(this.receivedData);
				if (this.receivedData == '选择地址') {
					uni.$emit('selectOtherAddr', {
						item
					})
					uni.navigateBack({})
				}
			},
			async getAllAddr() {
				const res = await util.request(api.addressListUrl, {}, 'POST')
				console.log(res)
				if (res.code !== 0) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				} else {
					this.addrList = res.data || []
				}
			},
			async defaultChange(item) {
				if (item.isDefault === '0') {
					item.isDefault = '1'
				} else {
					item.isDefault = '0'
				}
				const res = await util.request(api.setAddressDefaultUrl + '?id=' + item.id, {}, 'POST')
				console.log(res)
				if (res.code !== 0) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				} else {
					uni.showToast({
						title: '设置默认地址成功',
						icon: 'none'
					})
				}
			},
			editEvent(item) {
				uni.navigateTo({
					url: `/pages/address/addAddress?id=${item.id}`
				})
			},
			deleteEvent(item) {
				util.request(api.deleteAddressUrl + '?id=' + item.id, {}, 'POST').then(res => {
					console.log(res)
					if (res.code !== 0) {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					} else {
						this.getAllAddr()
					}
				})
			},
			addAddress() {
				uni.navigateTo({
					url: `/pages/address/addAddress`
				})
			}
		}
	}
</script>

<style lang="scss">
	.address-manager-page {
		padding: 20rpx 0;
		background-color: #fff;

		.add-addr {
			display: flex;
			justify-content: flex-end;
			color: #4c7fff;
			margin-bottom: 20rpx;
			padding: 0 20rpx;
		}

		.address-list {
			.each-address {
				background-color: #fff;
				padding: 20rpx;
				margin-bottom: 20rpx;
				border-bottom: 1rpx solid #e5e5e5;

				.name-phone {
					display: flex;
					align-items: center;
					justify-content: space-between;
				}

				.addr {
					color: #a7a7a7;
					margin: 20rpx 0;
				}

				.opera-btns {
					display: flex;
					align-items: center;
					justify-content: space-between;

					.left-default-btn {
						.default-addr {
							font-size: 28rpx;
						}

						.custom-checkbox {
							::v-deep .uni-checkbox-input {
								appearance: none;
								-webkit-appearance: none;
								border-radius: 50%;
								border: 1px solid #ccc;
								width: 20px;
								height: 20px;
								outline: none;
								position: relative;
							}

							::v-deep .uni-checkbox-input-checked {
								background-color: #4c7fff;
								border-color: #4c7fff;
							}

							::v-deep .uni-checkbox-input-checked::before {
								position: absolute;
								top: 50%;
								left: 50%;
								transform: translate(-50%, -50%);
								color: #fff;
								font-size: 14px;
								/* 调整字体大小 */
								font-weight: bold;
								/* 确保字体加粗 */
								line-height: 1;
								/* 调整行高以确保对号居中 */
							}
						}
					}

					.right-edit-delete {
						.btn-text {
							display: inline-flex;
							align-items: center;
							margin-left: 20rpx;
							font-size: 28rpx;

							.icon-edit-delete {
								width: 48rpx;
								height: 48rpx;
							}
						}
					}
				}
			}
		}
	}
</style>